package io;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.OutputStreamWriter;

import org.junit.Test;

/**
 * 写时从字符流转换为字节流的演示 读时从字节流转换为字符流的演示
 * 
 * @author fidel
 */
public class ByteCharacterInterchangeDemo {
    @Test
    public void test() {
        File file = new File(TestResourcesDirectory.PATH + File.separator + "ByteCharacterInterchangeDemo.txt");
        try (OutputStream os = new FileOutputStream(file); OutputStreamWriter osw = new OutputStreamWriter(os);) {
            char[] chars = new char[] { 'J', 'a', 'v', 'a', '标', '准', '版' };
            osw.write(chars, 0, chars.length);
            osw.write(System.getProperty("line.separator"));
            osw.write("输入/输出流");
            osw.write(System.lineSeparator());
            osw.write("字节/字符流");
            osw.flush();
            os.flush();
            InputStream is = new FileInputStream(file);
            InputStreamReader isr = new InputStreamReader(is);
            if (isr.ready()) {
                int value;
                while ((value = isr.read()) > -1) {
                    System.out.print((char) value);
                }
            }
            isr.close();
            is.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
